package com.sixuan.rabbitmqprovider.workmode.publishandsubscribe;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * RabbitMq工作模式：publish/subscribe模式（发布订阅模式）
 * 1.一个生产者将消息发送给交换机；
 * 2.交换机绑定了多个队列，每个队列绑定了自己对应的消费者（不止一个，多个的话，这几个消费者达成WorkQueues模式）；
 * 3.生产者将消息发送给交换机，交换机转发消息到与其绑定的队列上，交换机的每个队列都会收到这个消息。
 * Created by LENOVO on 2020-7-8 17:18
 *
 * @author LENOVO
 */
public class Provider {

    private static final String rabbitMqHost = "47.97.195.191";
    private static final int rabbitMqPort = 5672;
    private static final String username = "guest";
    private static final String password = "guest";

    private static final String EMAIL_QUEUE_INFORMATION = "email_queue_information";
    private static final String SMS_QUEUE_INFORMATION = "sms_queue_information";
    private static final String EXCHANGE_FANOUT_INFORMATION = "exchange_fanout_information";

    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(rabbitMqHost);
        connectionFactory.setPort(rabbitMqPort);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");

        Connection connection = null;
        Channel channel = null;
        try {
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();
            //声明交换机,参数：String exchange, String type
            /**
             * 1.String exchange：交换机名称
             * 2.String type 交换机类型
             * BuiltinExchangeType:四种类型
             * 1.FANOUT:发布订阅类型（publish/subscribe）
             * 2.DIRECT:对应Routing模式
             * 3.HEADERS:对应Headers模式
             * 4.TOPIC:对应Topic模式
             */
            channel.exchangeDeclare(EXCHANGE_FANOUT_INFORMATION, BuiltinExchangeType.FANOUT);
            //声明队列
            //email队列
            channel.queueDeclare(EMAIL_QUEUE_INFORMATION, true, false, false, null);
            //sms队列
            channel.queueDeclare(SMS_QUEUE_INFORMATION, true, false, false, null);

            //将队列绑定到交换机上,参数：String queue, String exchange, String routingKey
            /**
             * 1.String queue:队列名称
             * 2.String exchange:交换机名称
             * 3.String routingKey:路由key，交换机根据路由key将消息转发到指定队列，发布订阅模式为空串
             */
            //绑定email
            channel.queueBind(EMAIL_QUEUE_INFORMATION, EXCHANGE_FANOUT_INFORMATION, "");
            //绑定sms
            channel.queueBind(SMS_QUEUE_INFORMATION, EXCHANGE_FANOUT_INFORMATION, "");

            //发送消息
            for (int i = 0; i < 9; i++) {
                String msg = "Hello World!" + i;
                //指定交换机就行了，交换机会转发到绑定的队列上。
                channel.basicPublish(EXCHANGE_FANOUT_INFORMATION, "", null, msg.getBytes());
                System.out.println("Send to consumer finished ! message：" + msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
